define(['form'], function(require, exports, module) {

    function bindFrom(config) {
        $('#js-bind-form').on('submit', function() {
            $('.error').html('');
            var _this = $(this);
            _this.ajaxSubmit({
                url: '/Ajax/Passport/registerOrLoginBindOAuth?stmp=' + new Date().toString(),
                type: 'post',
                dataType: 'json',
                success: function(d) {
                    if (d.error) {
                        var code = d.code;
                        var msg = d.data;
                        if (code == 317) {
                            window.location.href = config.fromUrl;
                        } else if (code == 316) {
                            window.location.href = '/register.html?fromurl=' + decodeURIComponent(config.fromUrl);
                        } else if (code == 304 || code == 301) {
                            _this.find('input[name="email"]').next().html(msg);
                        } else if (code == 310) {
                            _this.find('input[name="password"]').next().html(msg);
                        } else if (code == 315) {
                            _this.find('input[name="username"]').next().html(msg);
                        } else {
                            alert(msg);
                        }
                    } else {
                        window.location.href = config.fromUrl;
                    }
                }
            });
            return false;
        });
    }

    exports.init = function(config) {
        bindFrom(config);
    }
});